package com.longrise.android.byjk.permission;

import android.Manifest;
import android.app.Activity;
import android.content.Context;
import android.content.pm.PackageManager;
import android.os.Build;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.text.TextUtils;

import com.longrise.common.utils.PrintLog;


/**
 * Created by callxkj on 2016/11/24.
 * 获取权限的工具类
 * 只需要在相应的Activity内复写onRequestPermissionsResult
 */

public class PermissionUtil {

    private static final String TAG = "PermissionUtil";
    private static PermissionUtil mPermissionUtil;
    private Activity mActivity;
    private Permission mPerm;
    private String mRationale;
    private String mTitle;
    private PermissionCallBack mCallBack;

    public static PermissionUtil getInstance() {
        if (mPermissionUtil == null) {
            mPermissionUtil = new PermissionUtil();
        }
        return mPermissionUtil;
    }

    private PermissionUtil() {
    }

    public void requestPermission(Activity activity, Permission perm, String title, String rationale, PermissionCallBack callBack) {
        this.mActivity = activity;
        this.mPerm = perm;
        this.mRationale = rationale;
        this.mTitle = title;
        this.mCallBack = callBack;

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            if (hasPermission(activity, perm.getPermission())) {
                mCallBack.onSuccess();
            } else {
                requestPermission(activity, perm.getPermission(), perm.getRequestCode());
            }
        } else {
            callBack.onSuccess();
        }
    }

    private boolean hasPermission(Context context, String permission) {
        if (PackageManager.PERMISSION_GRANTED == ContextCompat.checkSelfPermission(context, permission)) {
            return true;
        } else {
            return false;
        }
    }

    private void requestPermission(Activity activity, String permission, int requestCode) {
        ActivityCompat.requestPermissions(activity, new String[]{permission}, requestCode);
    }


    public void onPermissionsGranted() {
        if (mCallBack != null) {
            mCallBack.onSuccess();
        }
        PrintLog.d(TAG, "onPermissionsGranted");
    }


    public void onPermissionsDenied() {
        PrintLog.d(TAG, "onPermissionsDenied");
        if (mCallBack != null) {
            mCallBack.onDenied();
        }
        if (!TextUtils.isEmpty(mRationale)) {
            new PermissionDialog.Builder(mActivity, mRationale).setRequestCode(mPerm.getRequestCode()).build().show();
        }
    }

    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
            onPermissionsGranted();
        } else {
            onPermissionsDenied();
        }
    }

    public interface PermissionCallBack {
        void onSuccess();

        void onDenied();
    }

    public enum Permission {
        CAMERA(Manifest.permission.CAMERA, 111),
        STORAGE(Manifest.permission.WRITE_EXTERNAL_STORAGE, 112),
        CALENDAR(Manifest.permission.READ_CALENDAR, 113),
        CONTACTS(Manifest.permission.READ_CONTACTS, 114),
        LOCATION(Manifest.permission.ACCESS_FINE_LOCATION, 115),
        MICROPHONE(Manifest.permission.RECORD_AUDIO, 116),
        PHONE(Manifest.permission.CALL_PHONE, 117),
        SENSORS(Manifest.permission.BODY_SENSORS, 118),
        SMS(Manifest.permission.SEND_SMS, 119);

        private final String permission;
        private final int requestCode;

        Permission(String permission, int requestCode) {
            this.permission = permission;
            this.requestCode = requestCode;
        }

        public String getPermission() {
            return permission;
        }

        public int getRequestCode() {
            return requestCode;
        }

    }

}
